//
//  dpglViewController.m
//  LazyCat
//
//  Created by rocky on 15/12/14.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "dpglViewController.h"
#import "tabObj.h"
#import "subYHQController.h"

@interface dpglViewController ()<UIPageViewControllerDataSource,UIPageViewControllerDelegate,UIScrollViewDelegate>

{
    UIPageViewController *_pageViewCtrl;
    NSMutableArray *_dataArray;
    NSInteger _currentPage;
    UIView *_bgView;
    UIView *_lineView;
}

@end

@implementation dpglViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNav:@"优惠券" doBack:YES];
    UIView *lineV = [[UIView alloc]initWithFrame:CGRectMake(0, 63, ScreenWidth, 1)];
    lineV.backgroundColor = [UIColor lightGrayColor];
    lineV.alpha = 0.2;
    [self.view addSubview:lineV];
    [self initRightBtn];//添加右边的按钮
    [self createTopView];//添加头顶的按钮
    [self createPageVC];//添加pageVC
}
-(void)createTopView {//添加头顶的按钮
    _bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, 40)];
    _bgView.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:_bgView];
    _bgView.alpha = 1;
    _dataArray = [[NSMutableArray alloc]init];
    NSArray *array = @[@"未开始",@"进行中",@"已失效"];
    for(int i=0;i<array.count;i++){
        tabObj *tab = [[tabObj alloc]init];
        tab.title = array[i];   
        tab.subVC = [[subYHQController alloc]init];
        tab.subVC.currentPage = i;
        [_dataArray addObject:tab];
        __weak typeof(self)wSubVC = self;
        [tab.subVC setMyBlock:^(UIViewController * editVC) {
            [wSubVC.navigationController pushViewController:editVC animated:YES];
        }];
    }
    _currentPage = 0;
    _lineView = [[UIView alloc]init];
    _lineView.backgroundColor = SetColor(255,179, 14, 1);
    [_bgView addSubview:_lineView];
    for(int i=0;i<_dataArray.count;i++){
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i*(ScreenWidth/_dataArray.count), 0, ScreenWidth/_dataArray.count, 40);
        [btn setTitle:array[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn setTitleColor:SetColor(255,179, 14, 1) forState:UIControlStateSelected];
        [btn setBackgroundColor:[UIColor clearColor]];
        [btn addTarget:self action:@selector(changeTabVC:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = 100 + i;
        [_bgView addSubview:btn];
        if(i==0){
            btn.selected = YES;
            _lineView.frame = CGRectMake(0, 38, ScreenWidth/3, 2);
        }
    }
}
//单击按钮时切换视图
- (void)changeTabVC:(UIButton *)sender {
    NSInteger index = sender.tag-100;
    subYHQController *sub = [_dataArray[index] subVC];
    __weak typeof(_bgView)wBgView = _bgView;
    __weak typeof(_lineView)wLineView = _lineView;
    //显示翻页后要显示sub，翻页方向根据单击的按钮决定
    [_pageViewCtrl setViewControllers:@[sub] direction:_currentPage>index animated:YES completion:^(BOOL finished) {
        _currentPage = index;
        for(UIView *view in wBgView.subviews){
            if([view isKindOfClass:[UIButton class]]){
                UIButton *tb = (UIButton *)view;
                if(tb.tag == sender.tag){
                    tb.selected=YES;
                }else {
                    tb.selected = NO;
                }}}
        wLineView.frame = CGRectMake(sender.frame.origin.x, 38, ScreenWidth/3, 2);
    }];
}
-(void)createPageVC {//添加pageVC
    _pageViewCtrl = [[UIPageViewController alloc]initWithTransitionStyle:(UIPageViewControllerTransitionStyleScroll) navigationOrientation:(UIPageViewControllerNavigationOrientationHorizontal) options:nil];
    subYHQController * sub = [_dataArray[_currentPage] subVC];
    [_pageViewCtrl setViewControllers:@[sub] direction:(UIPageViewControllerNavigationDirectionForward) animated:YES completion:^(BOOL finished) {
        
    }];
    _pageViewCtrl.view.frame = CGRectMake(0, 40+64, ScreenWidth, ScreenHeight-40-64);
    [self.view addSubview:_pageViewCtrl.view];
    _pageViewCtrl.dataSource = self;
    _pageViewCtrl.delegate = self;
    //获取显示所有view的滚动视图
    UIScrollView *sv = _pageViewCtrl.view.subviews[0];
    sv.delegate = self;
    [_pageViewCtrl.view.subviews[0] setDelegate:self];
}
-(void)initRightBtn {//添加右边的按钮
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addBtn setTitle:@"添加" forState:UIControlStateNormal];
    addBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [addBtn setTitleColor:[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0] forState:UIControlStateNormal];
    addBtn.frame = CGRectMake(ScreenWidth-47, 30, 40, 30);
    [addBtn addTarget:self action:@selector(addYHQ) forControlEvents:UIControlEventTouchUpInside];//跳到添加优惠券页面
    [self.navBar addSubview:addBtn];
}
#pragma mark --代理方法
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
    subYHQController *sub = (subYHQController *)viewController;
    NSInteger index = sub.currentPage;
    index++;
    if(index>=_dataArray.count){
        index = 0;
        return nil;
    }
    subYHQController *rsvc = [_dataArray[index]subVC];
    return rsvc;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{
    subYHQController *sub = (subYHQController *)viewController;
    NSInteger index = sub.currentPage;
    index--;
    if(index<0){
        index = _dataArray.count-1;
        return nil;
    }
    subYHQController *rsvc = [_dataArray[index]subVC];
    return rsvc;
    
}
//翻页成功后执行的方法
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{
    subYHQController *sub = (subYHQController *)pageViewController.viewControllers[0];
    _currentPage = sub.currentPage;
    for(UIView *view in _bgView.subviews){
        if([view isKindOfClass:[UIButton class]]){
            UIButton *btn = (UIButton *)view;
            if(btn.tag-100==_currentPage){
                btn.selected =YES;
            }else {
                btn.selected = NO;
            }
        }
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offSetX = scrollView.contentOffset.x;
    CGFloat ratio = ScreenWidth/3/ScreenWidth;
    CGRect frame = _lineView.frame;
    frame.origin.x = (offSetX-ScreenWidth)*ratio+_currentPage*ScreenWidth/3;
    _lineView.frame = frame;
}
-(void)addYHQ {//跳到添加优惠券页面
    addYHQViewController *addVc = [[addYHQViewController alloc]init];
    [self.navigationController pushViewController:addVc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
@end
